package game.mapviewer.popups.battle 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import game.mapviewer.popups.Popup;
	import game.phases.actionphase.commands.BattleCalculator;
	import game.unitmanager.ArmyController;
	/**
	 * ...
	 * @author rnk
	 */
	public class BattlePopup extends Popup
	{
		private var bg:MovieClip;
		private var armyAttacker:ArmyView;
		private var armyDefender:ArmyView;
		private var panel:MovieClip;
		
		public function BattlePopup() 
		{
			
		}
		
		override public function Init():void 
		{
			super.Init();
			
			bg = new battlePopup();
			addChild(bg);
			
			var attacker:ArmyController = params.attacker;
			var defender:ArmyController = params.defender;
			var provinceName:String = defender.model.province.name;
			
			bg.battleText.text = "Battle at " + provinceName;
			
			armyAttacker = new ArmyView(attacker.loyalty, attacker.model.units);
			armyDefender = new ArmyView(defender.loyalty, defender.model.units);
			
			var armyHolder:Sprite = new Sprite();
			bg.addChild(armyHolder);
			armyHolder.x = 153;
			armyHolder.y = 223;
			armyHolder.scaleX = -1.0;
			armyHolder.mask = bg.attackerMask;
			armyHolder.addChild(armyAttacker);
			
			armyHolder = new Sprite();
			bg.addChild(armyHolder);
			armyHolder.x = 490;
			armyHolder.y = 223;
			armyHolder.mask = bg.defenderMask;
			armyHolder.addChild(armyDefender);
			
			RecalculatePowers();
		}
		
		private function RecalculatePowers():void 
		{
			bg.attackerPowerText.text = String(armyAttacker.GetPower(true));
			bg.defenderPowerText.text = String(armyDefender.GetPower(false));
		}
		
		override public function Die():void 
		{
			super.Die();
		}
		
		public function AskAttackOrNot(callback:Function):void
		{
			//show panel
			panel = new wantToAttackPanel();
			panel.x = 0;
			panel.y = 344;
			bg.addChild(panel);
			
			//set listeners
			panel.yesButton.addEventListener(MouseEvent.CLICK, OnAONButtonClick);
			panel.noButton.addEventListener(MouseEvent.CLICK, OnAONButtonClick);
			
			
			//call callback with answer
			function OnAONButtonClick(e:MouseEvent):void 
			{
				panel.yesButton.removeEventListener(MouseEvent.CLICK, OnAONButtonClick);
				panel.noButton.removeEventListener(MouseEvent.CLICK, OnAONButtonClick);
				var result:Boolean = false;
				switch (e.target) 
				{
					case panel.yesButton:
						result = true;
					break;
					case panel.noButton:
						result = false;
					break;
				}
				bg.removeChild(panel);
				panel = null;
				callback(result);
				
			}
		}
		
		public function AskTactics(playerUid:int, power:int,isAttacking:Boolean, callback:Function):void
		{
			//show panel
			panel = new chooseTacticsPanel();
			panel.x = 0;
			panel.y = 346;
			bg.addChild(panel);
			
			//show powers
			panel.aggressivePowerText.text = String(BattleCalculator.GetTacticsPowerText(power, 0));
			panel.carefulPowerText.text = String(BattleCalculator.GetTacticsPowerText(power, 1));
			panel.defensivePowerText.text = String(BattleCalculator.GetTacticsPowerText(power, 2));
			
			//show player name
			papa.papa.papa.gui.SetCurrentPlayer(papa.papa.papa.GetPlayer(playerUid));
			
			//set listeners
			panel.aggressiveButton.addEventListener(MouseEvent.CLICK, OnATButtonClick);
			panel.carefulButton.addEventListener(MouseEvent.CLICK, OnATButtonClick);
			panel.defensiveButton.addEventListener(MouseEvent.CLICK, OnATButtonClick);
			
			
			//call callback with answer
			function OnATButtonClick(e:MouseEvent):void 
			{
				panel.aggressiveButton.removeEventListener(MouseEvent.CLICK, OnATButtonClick);
				panel.carefulButton.removeEventListener(MouseEvent.CLICK, OnATButtonClick);
				panel.defensiveButton.removeEventListener(MouseEvent.CLICK, OnATButtonClick);
				var result:int = 0;
				switch (e.target) 
				{
					case panel.aggressiveButton:
						result = 0;
						
					break;
					case panel.carefulButton:
						result = 1;
					break;
					case panel.defensiveButton:
						result = 2;
					break;
				}
				papa.papa.papa.gui.SetCurrentPlayer(null);
				bg.removeChild(panel);
				panel = null;
				callback(result);
				
			}		
		}
		
		public function AskSupport(armyController:ArmyController, callback:Function):void
		{
			//show panel
			panel = new supportPanel();
			panel.x = 0;
			panel.y = 344;
			bg.addChild(panel);
			
			//show powers
			panel.attackerPowerText.text = String(armyController.GetPower(true));
			panel.defenderPowerText.text = String(armyController.GetPower(false));
			
			//show player name
			papa.papa.papa.gui.SetCurrentPlayer(papa.papa.papa.GetPlayer(armyController.loyalty));
			
			//set listeners
			panel.supportAttackerButton.addEventListener(MouseEvent.CLICK, OnASButtonClick);
			panel.supportDefenderButton.addEventListener(MouseEvent.CLICK, OnASButtonClick);
			panel.cancelButton.addEventListener(MouseEvent.CLICK, OnASButtonClick);
			
			
			//call callback with answer
			function OnASButtonClick(e:MouseEvent):void 
			{
				panel.supportAttackerButton.removeEventListener(MouseEvent.CLICK, OnASButtonClick);
				panel.supportDefenderButton.removeEventListener(MouseEvent.CLICK, OnASButtonClick);
				panel.cancelButton.removeEventListener(MouseEvent.CLICK, OnASButtonClick);
				var result:int = 3;
				switch (e.target) 
				{
					case panel.supportAttackerButton:
						result = 0;
						//add army units and banner to army view
						armyAttacker.AddUnits(armyController.model.units);
						armyAttacker.AddBanner(armyController.loyalty);
						
					break;
					case panel.supportDefenderButton:
						result = 1;
						//add army units and banner to army view
						armyDefender.AddUnits(armyController.model.units);
						armyDefender.AddBanner(armyController.loyalty);
					break;
				}
				RecalculatePowers();
				papa.papa.papa.gui.SetCurrentPlayer(null);
				bg.removeChild(panel);
				panel = null;
				callback(result);
				
			}			
			
		}
		
		public function ShowBattleResults(winner:int, attacker:Object, defender:Object, callback:Function):void
		{
			//show panel
			panel = new battleResultPanel();
			panel.x = 37;
			panel.y = 101;
			bg.addChild(panel);
			
			//show stats
			var tacticsText:String = attacker.tactics == 0?"Aggresive":attacker.tactics == 1?"Careful":"Defensive";
			
			panel.stat1.text = 
				(attacker.tactics == 0?"Aggresive":attacker.tactics == 1?"Careful":"Defensive") + "\n" +
				"power " +attacker.power+ "\n" +
				"damage " +attacker.damage+ "\n" +
				"casualties "+attacker.casualties
			;
			panel.stat2.text = 
				(defender.tactics == 0?"Aggresive":defender.tactics == 1?"Careful":"Defensive") + "\n" +
				"power " +defender.power+ "\n" +
				"damage " +defender.damage+ "\n" +
				"casualties "+defender.casualties
			;
			switch (winner) 
			{
				case 0:
					panel.winnerText.text = "Victor is " + papa.papa.papa.GetPlayer(attacker.player).name;
					papa.papa.papa.gui.SetCurrentPlayer( papa.papa.papa.GetPlayer(attacker.player));
				break;
				case 1:
					panel.winnerText.text = "Victor is " + papa.papa.papa.GetPlayer(defender.player).name;
					papa.papa.papa.gui.SetCurrentPlayer(papa.papa.papa.GetPlayer(defender.player));
				break;
				default:
					panel.winnerText.text = "Draw!";
					papa.papa.papa.gui.SetCurrentPlayer(null);
				break;
			}
			
			
			//set listeners
			panel.okButton.addEventListener(MouseEvent.CLICK, OnBRButtonClick);
			
			
			
			//call callback with answer
			function OnBRButtonClick(e:MouseEvent):void 
			{
				panel.okButton.removeEventListener(MouseEvent.CLICK, OnBRButtonClick);
				papa.papa.papa.gui.SetCurrentPlayer(null);
				bg.removeChild(panel);
				panel = null;
				callback();
				
			}	
		}
		
		
		
	}

}